import axios from 'axios' //引入 axios
import {
    Notify
} from 'vant';
import store from '@/store'

// create an axios instance   创建axios实例
const service = axios.create({
    baseURL: 'https://ds.pushiyixue.com',
    timeout: 15000,
    responseType: "json",
    // withCredentials: true,
    headers: {
        "Content-Type": "application/json;charset=utf-8"
    }
})

service.interceptors.request.use(
    config => {
        // if (sessionStorage.getItem('userToken')) {
        //     config.headers.token = sessionStorage.getItem('userToken');
        // }
        // config.cancelToken = new axios.CancelToken(function (cancel) {
        //     store.commit('user/pushToken', {
        //         cancelToken: cancel
        //     })
        // })
        return config;
    },
    error => {
        Notify({
            type: 'danger',
            message: '请求失败'
        });
        return Promise.reject(error);
    }
)

service.interceptors.response.use(
    response => {
        if (response.status === 200) {
            const res = response.data
            return Promise.resolve(res);
        } else {
            return Promise.reject(response);
        }
    },
    error => {
        Notify({
            type: 'danger',
            message: '请求失败'
        });
        return Promise.reject(error);
    })

export default service